//
//  CitysPresenter.swift
//  YunshuApp
//
//  Created by 周文杰 on 2017/6/2.
//  Copyright © 2017年 zwj. All rights reserved.
//

import UIKit

class CityPresenter: NSObject ,UITableViewDelegate, UITableViewDataSource{

    var singleSelect = false

    let view = UITableView.init(frame: CGRect.init(x: 0, y: 0,
                                                   width: Const.SCREEN_WIDTH,
                                                   height: Const.SCREEN_HEIGHT),
                                style: .plain)
    var models = ["全部","北京市","天津市","河北省","山西省","内蒙古自治区","辽宁省","吉林省","黑龙江省","上海市","江苏省","浙江省","安徽省","福建省","江西省","山东省","河南省","湖北省","湖南省","广东省","广西壮族自治区","海南省","四川省","贵州省","云南省","重庆市","西藏自治区","陕西省","甘肃省","青海省","宁夏回族自治区","新疆维吾尔自治区","香港特别行政区","澳门特别行政区","台湾省"]
    var pageNum = 0
    
    var selected:[String] = []
    
    override init() {
        super.init()
        
        self.view.delegate = self
        self.view.dataSource = self
        
        self.view.reloadData()
        
    }
    
    // MARK :- tableView
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return models.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let reuseId = "banner"
        let model = models[indexPath.row]
        if let cell = tableView.dequeueReusableCell(withIdentifier: reuseId) as? KXChannelCell {
            cell.button.isSelected = self.selected.contains(model)
            cell.textLabel?.text = model
            cell.selectionStyle = .none
            return cell
        }
        let cell = KXChannelCell.init(style: .default, reuseIdentifier: reuseId)
        cell.contentView.backgroundColor = UIColor.white
        cell.textLabel?.text = model
        cell.button.isSelected = self.selected.contains(model)
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let model = self.models[indexPath.row]
        if singleSelect {
            selected = [self.models[indexPath.row]]
        }else{
            if selected.contains(model) {
                selected.removeAll(model)
            }else{
                selected.append(model)
            }
        }
        self.view.reloadData()
    }
}
